package com.project.finance.role.controller;

import com.project.finance.common.po.TbSysRole;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.role.service.RoleService;
import com.sun.deploy.nativesandbox.comm.Request;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//角色管理
@Slf4j
@RestController
@RequestMapping("/api")
public class RoleController {
    private static final Logger logger = LogManager.getLogger(RoleController.class);
    @Autowired
    private RoleService roleService;
    //查询所有角色
//    @RequestMapping(value = "/roles",method = RequestMethod.GET)
//    private Result listRole(){
//        List<TbSysRole> tbSysRoles=roleService.selectAll();
//        return ResultGenerator.apiSuccess(tbSysRoles);
//    }
    //角色增加
    @RequestMapping(value = "/role",method = RequestMethod.POST)
    private Result insert(@RequestBody TbSysRole tbSysRole){
        int i=roleService.insert(tbSysRole);
        if (i>0){  // 添加成功
            return ResultGenerator.apiSuccess("ok");
        }else {
            return ResultGenerator.apiError("error");
        }

    }
    //删除角色
    @RequestMapping(value = "/role",method = RequestMethod.DELETE)
    private Result delete(@RequestParam("roleId") int roleId){
        int d=roleService.delete(roleId);
            return ResultGenerator.apiSuccess("ok");

    }
    //角色修改
    @RequestMapping(value = "/role",method = RequestMethod.PUT)
    private Result update(@RequestBody TbSysRole tbSysRole){
        int u=roleService.update(tbSysRole);
        if (u>0){
            return ResultGenerator.apiSuccess("ok");
        }else {
            return ResultGenerator.apiError("error");
        }
    }
    //分页
    @RequestMapping(value = "/role",method = RequestMethod.GET)
    public ResultMsg listRoleByPageCondition(@RequestParam("pageSize")int pageSize,
                                             @RequestParam("pageNum")int pageNum){
        ResultMsg resultMsg = roleService.listRoleByPage(pageSize, pageNum);
        resultMsg.setCode(0);
        return resultMsg;
    }
    //根据账号来查询用户信息
    @RequestMapping(value = "roles",method = RequestMethod.GET)
    public Result getSysRole(@RequestParam("name")String rolename){
        logger.info("GET Name : " + rolename);
        List<TbSysRole> tbSysRoles=roleService.getSysRole(rolename);
        Result listResult = ResultGenerator.apiSuccess(tbSysRoles);
        return listResult;
    }
}
